package com.water.expert.extraction.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.water.expert.extraction.repository.Extraction;
import com.water.expert.extraction.repository.ExtractionResult;
import com.water.expert.extraction.repository.ExtractionResultRepository;
import com.water.expert.extraction.service.ExtractionResultService;
import com.water.expert.extraction.service.ExtractionService;

@RestController
@RequestMapping("/extraction-results")
public class ExtractionResultController {
	@Autowired
	private ExtractionResultRepository extractionResultRepository;
	@Autowired
	private ExtractionService extractiontService;
	@Autowired
	private ExtractionResultService extractionResultService;

	// 获取所有抽取结果
	@GetMapping
	public Iterable<ExtractionResult> getAllExtractionResults() {
		return extractionResultRepository.findAll();
	}

	// 根据ID获取抽取结果
	@GetMapping("/{id}")
	public ResponseEntity<ExtractionResult> getExtractionResultById(@PathVariable Long id) {
		return extractionResultRepository.findById(id).map(ResponseEntity::ok)
				.orElseGet(() -> ResponseEntity.notFound().build());
	}

	// 根据ID获取抽取结果
	@GetMapping("/extraction/{id}")
	public List<ExtractionResult> getExtractionResultByExtractionId(@PathVariable Long id) {
		List<ExtractionResult> result = extractionResultRepository.findByExtractionId(id);
		return result;
	}

	// 创建抽取结果
	@PostMapping
	public ExtractionResult createExtractionResult(@RequestBody ExtractionResult extractionResult) {
		return extractionResultRepository.save(extractionResult);
	}

	@PostMapping(("/generate"))
	public Iterable<ExtractionResult> genExtractionResultByExtractionId(@RequestParam Long extractionId) {
		Extraction extraction = extractiontService.getExtractionById(extractionId);
		return extractionResultService.genExtractionResultByExtraction(extraction);
	}
	
	// 更新抽取结果
	@PutMapping("/{id}")
	public ResponseEntity<ExtractionResult> updateExtractionResult(@PathVariable Long id,
			@RequestBody ExtractionResult updatedExtractionResult) {
		return extractionResultRepository.findById(id).map(ExtractionResult -> {
			updatedExtractionResult.setId(id);
			return ResponseEntity.ok(extractionResultRepository.save(updatedExtractionResult));
		}).orElseGet(() -> ResponseEntity.notFound().build());
	}

	// 删除抽取结果
	@DeleteMapping("/{id}")
	public ResponseEntity<Void> deleteExtractionResult(@PathVariable Long id) {
		return extractionResultRepository.findById(id).map(extractionResult -> {
			extractionResultRepository.deleteById(id);
			return ResponseEntity.ok().<Void>build();
		}).orElseGet(() -> ResponseEntity.notFound().build());
	}
}
